﻿using Hidistro.Core;
using Hidistro.UI.Common.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_Pager_3q : AscxTemplatedWebControl
    {
        private Repeater PList;
        private HtmlInputHidden wnxd_Pager_Max;
        protected override void AttachChildControls()
        {
            if (this.Total > 0)
            {
                this.PList = (Repeater)this.FindControl("PList");
                this.PList.ItemDataBound += PList_ItemDataBound;
                this.wnxd_Pager_Max = (HtmlInputHidden)this.FindControl("wnxd_Pager_Max");
                int start = 0;
                if (this.Index > 3) start = this.Index - 3;
                int len = 5;
                int mul = (int)Math.Ceiling((decimal)this.Total / this.Size);
                if (mul < 5) len = mul;
                int x = this.Index + 2;
                x = x - mul;
                if (x > 0)
                {
                    start -= x;
                    if (start < 0) start = 0;
                }
                List<Info> list = new List<Info>();
                for (int i = start; i < start + len; i++)
                {
                    Info info = new Info()
                    {
                        Index = i + 1,
                        Selected = this.Index == i + 1
                    };
                    list.Add(info);
                }
                this.wnxd_Pager_Max.Value = mul.ToString();
                this.PList.DataSource = list;
                this.PList.DataBind();
            }
        }
        void PList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HtmlAnchor div = (HtmlAnchor)e.Item.FindControl("div");
                Info info = (Info)e.Item.DataItem;
                if (info.Selected)
                {
                    div.Attributes.Add("class", "current");
                    div.HRef = "";
                }
                div.InnerText = info.Index.ToString();
            }
        }
        public new void EnsureChildControls()
        {
            base.EnsureChildControls();
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_Comment/wnxd_Pager_3q.ascx";
            }
            base.OnInit(e);
        }
        public int Index { get; set; }
        public int Size { get; set; }
        public int Total { get; set; }
        private class Info
        {
            public bool Selected { get; set; }
            public int Index { get; set; }
        }
    }
}
